package com.matrix.index.ui.anime

import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.matrix.basecore.arouter.ARouterUtils
import com.matrix.basecore.base.BaseActivity
import com.matrix.basecore.constant.Constant
import com.matrix.index.R
import com.matrix.index.databinding.ActivityAnimCategoryBinding
import com.matrix.index.net.IndexResult
import com.matrix.index.ui.anime.adapter.AnimCategoryAdapter
import com.matrix.index.ui.anime.adapter.AnimPreviewAdapter
import com.matrix.index.ui.anime.bean.Child
import com.matrix.index.ui.anime.bean.MoHuBean
import com.matrix.index.ui.anime.utils.AssetsUtils
import com.matrix.index.ui.anime.viewmodel.AnimVM

@Route(path = Constant.ARouterPath.AnimCategoryActivityPath)
class AnimCategoryActivity : BaseActivity<ActivityAnimCategoryBinding>() {

    private val vm: AnimVM by viewModels()
    private var roots: ArrayList<Child> = arrayListOf()
    private var position = 1
    private lateinit var adapter: AnimCategoryAdapter
    private lateinit var adapterPre: AnimPreviewAdapter
    private var previews: MutableList<String> = mutableListOf()
    private lateinit var rootChild: MutableList<Child>
    private var childPosition = 0
    private var num = 20

    override fun getViewBinding(): ActivityAnimCategoryBinding {
        enableEdgeToEdge()
        return ActivityAnimCategoryBinding.inflate(layoutInflater)
    }

    override fun initView() {
        position = intent.getIntExtra(Constant.ARouterString.POSITION, 1)
        mViewBinding.recyclerview.layoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL).apply {
                gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE //防止Item互换
            }

        adapterPre = AnimPreviewAdapter(R.layout.anim_preview_layout, previews)
        mViewBinding.recyclerview.adapter = adapterPre

        mViewBinding.rvCategory.layoutManager = LinearLayoutManager(this)
        adapter = AnimCategoryAdapter(R.layout.anim_category_layout, roots)
        mViewBinding.rvCategory.adapter = adapter
    }

    override fun initData() {
        var rootOrigin = AssetsUtils.getAssetsData(this)
        rootChild = rootOrigin[position].children
        mViewBinding.tvTitle.text = rootOrigin[position].label
        adapter.setList(rootChild)
        getWallData(getAnimCategory(childPosition), num)
    }

    private fun getAnimCategory(position: Int): String {
        val root = rootChild[position]
        return root.value
    }

    override fun initListener() {
        mViewBinding.ivBack.setOnClickListener { finish() }

        mViewBinding.ivRight.setOnClickListener {
            mViewBinding.dlLayout.let {
                if (it.isOpen) {
                    it.closeDrawer(GravityCompat.END)
                } else {
                    it.openDrawer(GravityCompat.END)
                }
            }
        }

        adapter.setOnItemClickListener { _, _, position ->
            num = 20
            childPosition = position
            previews.clear()
            adapterPre.setList(previews)
            getWallData(getAnimCategory(childPosition), num)
            mViewBinding.dlLayout.closeDrawer(GravityCompat.END)
        }
        adapterPre.loadMoreModule.setOnLoadMoreListener {
            num += 20
            getWallData(getAnimCategory(childPosition), num)
        }
        adapterPre.setOnItemClickListener { _, _, position ->
            ARouterUtils.startPreviewActivityPath(previews.toCollection(ArrayList()), position)
        }
    }

    /**
     * 获取数据
     */
    private fun getWallData(id: String, num: Int) {
        vm.getMoHuPaper(id, num).observe(this, { value ->
            if (value.code == IndexResult.SUCCESS) {
                var mohu = value.t as MoHuBean
                if (mohu.code == "200") {
                    previews.addAll(mohu.pic)
                    adapterPre.setList(previews)
                }
            } else if (value.code == IndexResult.FAIL) {
                ToastUtils.showShort("数据有误")
            }
        })
    }
}